package com.ruoyi.system.enservice.impl;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.dto.input.UserExamSubjectGetInput;
import com.ruoyi.system.dto.input.UserSubjectConditionGetInput;
import com.ruoyi.system.dto.output.UserExamInfoOutput;
import com.ruoyi.system.dto.output.UserExamSubjectOutput;
import com.ruoyi.system.dto.output.UserInformationOutput;
import com.ruoyi.system.dto.output.UserSubjectConditionOutput;
import com.ruoyi.system.enservice.IUserEnService;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static java.util.stream.Collectors.toList;

@Service
public class UserEnServiceImpl implements IUserEnService {

    @Autowired
    private IGaUserExamService gaUserExamService;

    @Autowired
    private IGaExamPaperService gaExamPaperService;

    @Autowired
    private IGaExamPaperSubjectService gaExamPaperSubjectService;

    @Autowired
    private IGaSubjectService gaSubjectService;

    @Autowired
    private IGaUserService gaUserService;

    public List<UserExamInfoOutput> getExamInfo(Long student){
        GaUserExam temp = new GaUserExam();
        temp.setUser(student);
        List<GaUserExam> list = gaUserExamService.selectGaUserExamList(temp);

        List<UserExamInfoOutput> result = list.stream().map(p -> {
            UserExamInfoOutput tmp = new UserExamInfoOutput();
            tmp.setExamPaper(p.getExamPaper());

            GaExamPaper examPaper = gaExamPaperService.selectGaExamPaperById(p.getExamPaper());
            tmp.setExamName(examPaper.getName());

            tmp.setExamIsFinsh(p.getIsFinish());
            tmp.setExamPos(p.getSubjectPos());
            return tmp;
        }).collect(toList());

        return result;
    }

    public UserExamSubjectOutput getExamSubject(UserExamSubjectGetInput dto){
        GaUserExam userExam = new GaUserExam();
        userExam.setUser(dto.getStudentId());
        userExam.setExamPaper(dto.getExamId());

        userExam = gaUserExamService.selectGaUserExamList(userExam).get(0);

        List<GaSubject> subjects;
        GaExamPaperSubject examPaperSubject = new GaExamPaperSubject();

        examPaperSubject.setExamPaper(dto.getExamId());
        subjects = gaExamPaperSubjectService.selectGaExamPaperSubjectList(examPaperSubject).stream()
                .map(it -> gaSubjectService.selectGaSubjectById(it.getSubject())).collect(toList());

        return new UserExamSubjectOutput()
                .setExamPos(userExam.getSubjectPos())
                .setExamInfofmation(subjects);
    }

    public UserSubjectConditionOutput getSubjectCondition(UserSubjectConditionGetInput dto){
        GaUserExam userExam = new GaUserExam();
        userExam.setUser(dto.getStudentId());
        userExam.setExamPaper(dto.getExamId());

        userExam = gaUserExamService.selectGaUserExamList(userExam).get(0);

        return new UserSubjectConditionOutput()
                .setExamPos(userExam.getSubjectPos())
                .setScore(userExam.getScore());
    }

    public UserInformationOutput getInformation(Long studentId){
        GaUser user = new GaUser();
        user.setId(studentId);

        user = gaUserService.selectGaUserList(user).get(0);

        return new UserInformationOutput()
                .setStudentId(user.getId())
                .setStudentName(user.getName())
                .setStudentTel(user.getTel())
                .setStudentData(user.getData());
    }
}
